﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Collections;
using HtmlAgilityPack;

namespace AnimeWebCrawler
{
    public class WebCrawler
    {
        private string[] _raw;
        public string[] ResultRaw
        {
            get { return _raw; }
            set { }
        }
        private string[] _url;
        public string[] ResultURL
        {
            get { return _url; }
            set { }
        }
        private string[] _text;
        public string[] ResultText
        {
            get { return _text; }
            set { }
        }      
        private string _absxpath;
        public string AbsoluteXPath
        {
            get { return _absxpath; }
            set { }
        }
        private string Page;
        private string XPath;

        public WebCrawler()
        {
            this._absxpath = "";
            this.Page = "";
            this.XPath = "";
        }
        public void SetXPath(string Path)
        {
            this.XPath = Path;
        }
        public void LoadHost(string Host)
        {
            string HostRequest = Host;
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(HostRequest);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();
            StreamReader PageSR = new StreamReader(response.GetResponseStream());
            this.Page = PageSR.ReadToEnd();
            PageSR.Close();
        }
        public void LinkCollect()
        {
            HtmlAgilityPack.HtmlDocument MainPage = new HtmlAgilityPack.HtmlDocument();
            MainPage.LoadHtml(Page);
            var Node = MainPage.DocumentNode.SelectNodes(this.XPath);
            this._absxpath = Node[0].XPath;
            string[] URL = new string[Node.Count];
            string[] Text = new string[Node.Count];
            string[] Raw = new string[Node.Count];
            for (int i = 0; i < Node.Count; i++)
            {
                URL[i] = Node[i].Attributes["href"].Value;
                Text[i] = Node[i].InnerHtml;
                Raw[i] = Node[i].OuterHtml;
            }
            this._raw = Raw;
            this._text = Text;
            this._url = URL;
        }
    }
}